County Census

Load appropriate packages

library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.4.4     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.0
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(tigris)
To enable caching of data, set `options(tigris_use_cache = TRUE)`
in your R script or .Rprofile.
library(sf) 
Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tidycensus) 
library(htmltools)
library(janitor) 

Attaching package: 'janitor'

The following objects are masked from 'package:stats':

    chisq.test, fisher.test
library(here)
here() starts at /cloud/project
library(mapview) 
library(leafsync)
library(leaflet.extras2)
Loading required package: leaflet
options(tigris_class = "sf")

Download your individual Census API key

myapikey <- "6915eb30a8f7e581eea519ad7e1ddfaeb7430e38"

census_api_key(myapikey, overwrite=TRUE)
To install your API key for use in future sessions, run this function with `install = TRUE`.

Choose the variables we want to focus on and use

myvars <- c(totalpop = "B01003_001",
            medincome = "B19013_001",
            medage = "B01002_001")

Pull and focus on TX counties (or state of your choosing)

tx_counties_withgeo <- get_acs(geography = "county",
                       variables = c(myvars),
                       state = "TX",
                       output = "wide",
                       geometry = TRUE)
Getting data from the 2017-2021 5-year ACS
Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.

  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |                                                                      |   1%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |============                                                          |  18%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |======================================================================| 100%
tx_counties_withgeo
Simple feature collection with 254 features and 8 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -106.6456 ymin: 25.83738 xmax: -93.50829 ymax: 36.5007
Geodetic CRS:  NAD83
First 10 features:
   GEOID                      NAME totalpopE totalpopM medincomeE medincomeM
1  48053      Burnet County, Texas     48424        NA      65363       4694
2  48057     Calhoun County, Texas     20367        NA      61887       9517
3  48341       Moore County, Texas     21494        NA      55543       3786
4  48185      Grimes County, Texas     28878        NA      59086       4414
5  48035      Bosque County, Texas     18275        NA      59328       3702
6  48471      Walker County, Texas     76506        NA      44104       1820
7  48407 San Jacinto County, Texas     27380        NA      46678       6190
8  48005    Angelina County, Texas     86584        NA      52377       3075
9  48443     Terrell County, Texas       979       314      47012       5245
10 48421     Sherman County, Texas      2357       250      55667       9007
   medageE medageM                       geometry
1     44.7     0.3 MULTIPOLYGON (((-98.45924 3...
2     37.9     0.7 MULTIPOLYGON (((-96.77168 2...
3     30.6     0.6 MULTIPOLYGON (((-102.163 36...
4     40.4     0.5 MULTIPOLYGON (((-96.18831 3...
5     46.3     0.5 MULTIPOLYGON (((-98.00068 3...
6     35.9     0.7 MULTIPOLYGON (((-95.86227 3...
7     45.3     0.4 MULTIPOLYGON (((-95.35886 3...
8     37.7     0.4 MULTIPOLYGON (((-95.00488 3...
9     54.2     7.9 MULTIPOLYGON (((-102.5669 3...
10    41.2     3.7 MULTIPOLYGON (((-102.1628 3...

Remove MOE columns - they all end with “M”

tx_counties_withgeo <- tx_counties_withgeo %>%
  select(-ends_with("M"))

tx_counties_withgeo
Simple feature collection with 254 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -106.6456 ymin: 25.83738 xmax: -93.50829 ymax: 36.5007
Geodetic CRS:  NAD83
First 10 features:
   GEOID                      NAME totalpopE medincomeE medageE
1  48053      Burnet County, Texas     48424      65363    44.7
2  48057     Calhoun County, Texas     20367      61887    37.9
3  48341       Moore County, Texas     21494      55543    30.6
4  48185      Grimes County, Texas     28878      59086    40.4
5  48035      Bosque County, Texas     18275      59328    46.3
6  48471      Walker County, Texas     76506      44104    35.9
7  48407 San Jacinto County, Texas     27380      46678    45.3
8  48005    Angelina County, Texas     86584      52377    37.7
9  48443     Terrell County, Texas       979      47012    54.2
10 48421     Sherman County, Texas      2357      55667    41.2
                         geometry
1  MULTIPOLYGON (((-98.45924 3...
2  MULTIPOLYGON (((-96.77168 2...
3  MULTIPOLYGON (((-102.163 36...
4  MULTIPOLYGON (((-96.18831 3...
5  MULTIPOLYGON (((-98.00068 3...
6  MULTIPOLYGON (((-95.86227 3...
7  MULTIPOLYGON (((-95.35886 3...
8  MULTIPOLYGON (((-95.00488 3...
9  MULTIPOLYGON (((-102.5669 3...
10 MULTIPOLYGON (((-102.1628 3...

Remove trailing “E” to clean up

colnames(tx_counties_withgeo) <- sub("E$", "", colnames(tx_counties_withgeo)) # $ means end of string only

tx_counties_withgeo
Simple feature collection with 254 features and 5 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -106.6456 ymin: 25.83738 xmax: -93.50829 ymax: 36.5007
Geodetic CRS:  NAD83
First 10 features:
   GEOID                       NAM totalpop medincome medage
1  48053      Burnet County, Texas    48424     65363   44.7
2  48057     Calhoun County, Texas    20367     61887   37.9
3  48341       Moore County, Texas    21494     55543   30.6
4  48185      Grimes County, Texas    28878     59086   40.4
5  48035      Bosque County, Texas    18275     59328   46.3
6  48471      Walker County, Texas    76506     44104   35.9
7  48407 San Jacinto County, Texas    27380     46678   45.3
8  48005    Angelina County, Texas    86584     52377   37.7
9  48443     Terrell County, Texas      979     47012   54.2
10 48421     Sherman County, Texas     2357     55667   41.2
                         geometry
1  MULTIPOLYGON (((-98.45924 3...
2  MULTIPOLYGON (((-96.77168 2...
3  MULTIPOLYGON (((-102.163 36...
4  MULTIPOLYGON (((-96.18831 3...
5  MULTIPOLYGON (((-98.00068 3...
6  MULTIPOLYGON (((-95.86227 3...
7  MULTIPOLYGON (((-95.35886 3...
8  MULTIPOLYGON (((-95.00488 3...
9  MULTIPOLYGON (((-102.5669 3...
10 MULTIPOLYGON (((-102.1628 3...

Choose a variable to focus on and create a map view

mapview(tx_counties_withgeo, zcol = "medage")

Customize colors

mapview(tx_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1)
Warning: Found less unique colors (9) than unique zcol values (151)! 
Interpolating color vector to match number of zcol values.

Turn off legends, hover text, popups

mapview(tx_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         legend = FALSE, 
         label = FALSE, 
         popup = FALSE)
Warning: Found less unique colors (9) than unique zcol values (151)! 
Interpolating color vector to match number of zcol values.

Customize your own labels

mylabel <- glue::glue("{tx_counties_withgeo$NAM} {tx_counties_withgeo$medage}")


mapview(tx_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         label = mylabel)
Warning: Found less unique colors (9) than unique zcol values (151)! 
Interpolating color vector to match number of zcol values.

Customize your pop-ups

mypopup <- glue::glue(
"<strong>{tx_counties_withgeo$NAM}</strong><br />
  Total Population: {tx_counties_withgeo$totalpop}<br />
   Median Age: {tx_counties_withgeo$medage}") %>% 
  lapply(htmltools::HTML)
head(mypopup)
[[1]]
<strong>Burnet County, Texas</strong><br />
Total Population: 48424<br />
 Median Age: 44.7

[[2]]
<strong>Calhoun County, Texas</strong><br />
Total Population: 20367<br />
 Median Age: 37.9

[[3]]
<strong>Moore County, Texas</strong><br />
Total Population: 21494<br />
 Median Age: 30.6

[[4]]
<strong>Grimes County, Texas</strong><br />
Total Population: 28878<br />
 Median Age: 40.4

[[5]]
<strong>Bosque County, Texas</strong><br />
Total Population: 18275<br />
 Median Age: 46.3

[[6]]
<strong>Walker County, Texas</strong><br />
Total Population: 76506<br />
 Median Age: 35.9
mapview(tx_counties_withgeo, zcol = "medage", 
         col.regions = RColorBrewer::brewer.pal(9, "Greens"), 
         alpha.regions = 1,
         popup = mypopup)
Warning: Found less unique colors (9) than unique zcol values (151)! 
Interpolating color vector to match number of zcol values.

Back to Home Page